using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace SecurityLinkUp
{
    public class Security
    {
        public static bool IsAdminUser()
        {
            // returns true if the current user is some form of admin
            if ((Roles.IsUserInRole("CommunityAdmin") || Roles.IsUserInRole("ChapterAdmin") || Roles.IsUserInRole("SuperAdmin")))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        // checks that the current user is some form of admin, if not transfer to noaccess.aspx
        public static void CheckAdminUser(Page p)
        {
            if (!IsAdminUser())
            {
                p.Response.Redirect("/NoAccess.aspx");
            }
        }

        // gets the conection string the user should use in connecting to the database based
        // on their user level
        public static string GetDBConnectionString()
        {
            string connstr = "";
            MembershipUser usr = Membership.GetUser();

            // start off as a limited user
            connstr = ConfigurationManager.ConnectionStrings["LimitedUser"].ConnectionString;

            // is the user logged in
            if (usr != null)
            {
                connstr = ConfigurationManager.ConnectionStrings["AuthenticatedUser"].ConnectionString;
            
                // see if the user is a chapter or community admin
                if ((Roles.IsUserInRole("ChapterAdmin")) || (Roles.IsUserInRole("CommunityAdmin")))
                {
                    connstr = ConfigurationManager.ConnectionStrings["AdminUser"].ConnectionString;
                }

                // is the user a super user
                if (Roles.IsUserInRole("SuperAdmin"))
                {
                    connstr = connstr = ConfigurationManager.ConnectionStrings["SuperAdminUser"].ConnectionString;
                }
            }

            // return the highest level the user has
            return connstr;
        }

        
    }
}
